import { getCurrentInstance, ref, watch, defineEmits } from 'vue';
import { isArray } from 'lodash';

export function usePropsTwoWayValue(propName, props) {
  const instance = getCurrentInstance();
  if (!props) {
    props = instance.props;
  }

  const modelValue = ref(null);
  if (props.hasOwnProperty(propName)) {
    const propValue = props[propName];
    modelValue.value = propValue;
    watch(
      () => props[propName],
      val => {
        modelValue.value = val;
      },
    );
    defineEmits(`update:${propName}`);
    watch(
      () => modelValue.value,
      val => instance.emit(`update:${propName}`, val),
    );
  }

  return modelValue;
}

export function usePropsTwoWayValues(propNames) {
  let returnValue = {};
  const instance = getCurrentInstance();
  const { props } = instance;

  propNames.forEach(propName => {
    returnValue[propName] = usePropsTwoWayValue(propName, props);
  });
  return returnValue;
}
